package com.qf.shrio2203.user.service.impl;

import com.qf.shrio2203.untils.SaltUtils;
import com.qf.shrio2203.user.entity.User;
import com.qf.shrio2203.user.dao.UserDao;
import com.qf.shrio2203.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shoush
 * @since 2023-02-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void regist(User user) {
        //注册时先生成随机盐字符串
        String salt = SaltUtils.getSalt(8);
        //设置salt到user对象
        user.setSalt(salt);
        String originalPwd = user.getPassword();
        //生成加密密码
        //创建shiro创建的加密对象
        //参数1 原密码 2 盐 3 hash次数
        Md5Hash md5Hash = new Md5Hash(originalPwd, salt, 1024);
        String sercetPwd = md5Hash.toHex();
        user.setPassword(sercetPwd);

        save(user);//保存到数据库

    }
}
